package com.ddl.eurekaclient.responsibilityMode.ultimate;

import com.ddl.eurekaclient.responsibilityMode.elementary.PreparationList;
import com.ddl.eurekaclient.responsibilityMode.elementary.Study;

import java.util.ArrayList;
import java.util.List;

public class FilterChain implements StudyPrepareFilter {

	private int pos = 0;

	private Study study;

	private List<StudyPrepareFilter> studyPrepareFilterList;

	public FilterChain(Study study) {
		this.study = study;
	}

	public void addFilter(StudyPrepareFilter studyPrepareFilter) {
		if (studyPrepareFilterList == null) {
			studyPrepareFilterList = new ArrayList<StudyPrepareFilter>();
		}

		studyPrepareFilterList.add(studyPrepareFilter);
	}

	@Override
	public void doFilter(PreparationList thingList, FilterChain filterChain) {
		// 所有过滤器执行完毕
		if (pos == studyPrepareFilterList.size()) {
			study.study();
		} else {
			StudyPrepareFilter studyPrepareFilter = studyPrepareFilterList.get(pos++);
			System.out.println(studyPrepareFilter.getClass().getName());
			studyPrepareFilter.doFilter(thingList, filterChain);
		}

	}

}
